import axios from "axios";
import {Notification} from "element-ui";

const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 10000
});


service.interceptors.request.use(
    config => {
        const authorization = localStorage.getItem('authorization')
        if (authorization) {
            config.headers['token'] = authorization;
        }
        return config
    },
    err => {
        console.log('err:' + err)
        return Promise.reject(err)
    })


service.interceptors.response.use(res => {
    let data = res.data;
    let code = data.code;
    if (code === 403) {
        this.$route.path = "/login";
    } else if (code === 500) {
        if (data.msg === 'Bad credentials') {
            Notification({
                title: '错误',
                message: "帐号密码错误!",
                type: "error"
            });
        } else {
            Notification({
                title: '错误',
                message: data.msg,
                type: "error"
            });
        }
        return Promise.reject(res.data);
    }
    return res.data;
})

export default service;

